n = set()

def p(y):
    m = 0
    for i in range(1, y // 2 + 1):
        if y % i == 0:
            m += 1
        if m > 1:
            break
    if m == 1:
        n.add(y)

x = int(input())
s = str(x)
while True:
    if x>9999 or x<1000:
        print('0')
        break
    for u in range(1, 11):
        if u < 5:
            for o in range(0, 3):
                s1 = int(s[o])
                p(s1)
        elif u > 4 and u < 8:
            for o in range(0, 2):
                s1 = s[o] + s[o + 1]
                s2 = int(s1)
                p(s2)
        elif u<10 and u>7:
            for o in range(0, 1):
                s1 = s[o] + s[o + 1]+s[o+2]
                s2 = int(s1)
                p(s2)
        elif u > 9:
            p(x)
    if not n:
        print('0')
        break
    else:
        print(max(n))
        break

